package saxion.speelveld.spaceengineer.view;

import java.util.Observable;
import java.util.Observer;

import saxion.speelveld.spaceengineer.R;
import saxion.speelveld.spaceengineer.controller.BitmapManager;
import saxion.speelveld.spaceengineer.model.Game;
import saxion.speelveld.spaceengineer.model.ResourceType;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ResourceHeader extends LinearLayout implements Observer
{
	private Game game = null;

	private TextView moneys, water, electric;

	private ProgressBar happiness;

	private ImageView happinessimg;

	public ResourceHeader(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		init(context);
	}

	public ResourceHeader(Context context)
	{
		super(context);
		init(context);
	}

	public void init(Context context)
	{
		LayoutInflater inflater = (LayoutInflater)
			context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.resourceheader, this);
		moneys = (TextView) findViewById(R.id.moneytxt);
		water = (TextView) findViewById(R.id.watertxt);
		electric = (TextView) findViewById(R.id.electrictxt);
		happiness = (ProgressBar) findViewById(R.id.happinessbar);
		happinessimg = (ImageView) findViewById(R.id.happinesimage);

	}

	public void setGame(Game game)
	{
		this.game = game;
		game.addObserver(this);

		moneys.setText(Integer.toString(game.getResourceAmount(ResourceType.MONEY)));
		water.setText(Integer.toString(game.getResourceAmount(ResourceType.WATER)));
		electric.setText(Integer.toString(game.getResourceAmount(ResourceType.ELECTRICITY)));
		int happinessamount = game.getResourceAmount(ResourceType.HAPPINESS);

		if (happinessamount > 50)
		{
			happinessimg.setImageBitmap(BitmapManager.getBitmapByResourceType(ResourceType.HAPPINESS, getContext()));
		}
		else
		{
			happinessimg.setImageBitmap(BitmapManager.getBitmapFromResources(R.drawable.tnewsadsmiley, getContext()));
		}
		happiness.setProgress(happinessamount);
	}

	@Override
	public void update(Observable arg0, Object arg1)
	{
		moneys.setText(Integer.toString(game.getResourceAmount(ResourceType.MONEY)));
		water.setText(Integer.toString(game.getResourceAmount(ResourceType.WATER)));
		electric.setText(Integer.toString(game.getResourceAmount(ResourceType.ELECTRICITY)));
		int happinessamount = game.getResourceAmount(ResourceType.HAPPINESS);

		if (happinessamount > 50)
		{
			happinessimg.setImageBitmap(BitmapManager.getBitmapByResourceType(ResourceType.HAPPINESS, getContext()));
		}
		else
		{
			happinessimg.setImageBitmap(BitmapManager.getBitmapFromResources(R.drawable.tnewsadsmiley, getContext()));
		}
		happiness.setProgress(happinessamount);
	}

	// @Override
	// protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
	// {
	// super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	//
	// DisplayMetrics displaymetrics = null;
	// GameField gf = null;
	// try
	// {
	// gf = (GameField) findViewById(R.id.gameField1);
	// gf.requestLayout();
	//
	// displaymetrics = new DisplayMetrics();
	// ((Activity) getContext()).getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
	// }
	// catch (Exception ex)
	// {
	//
	// }
	//
	// if (gf != null && displaymetrics != null && gf.getHeight() > 0)
	// {
	//
	// int desiredHeight = displaymetrics.heightPixels - (gf.getHeight() + 1);
	//
	// int widthMode = MeasureSpec.getMode(widthMeasureSpec);
	// int widthSize = MeasureSpec.getSize(widthMeasureSpec);
	// int heightMode = MeasureSpec.getMode(heightMeasureSpec);
	// int heightSize = MeasureSpec.getSize(heightMeasureSpec);
	//
	// int width;
	// int height;
	//
	// // Measure Width
	// if (widthMode == MeasureSpec.EXACTLY)
	// {
	// // Must be this size
	// width = widthSize;
	// }
	// else if (widthMode == MeasureSpec.AT_MOST)
	// {
	// // Can't be bigger than...
	// width = widthSize;
	// }
	// else
	// {
	// // Be whatever you want
	// width = widthMeasureSpec;
	// }
	//
	// // Measure Height
	// if (heightMode == MeasureSpec.EXACTLY)
	// {
	// // Must be this size
	// height = heightSize;
	// }
	// else if (heightMode == MeasureSpec.AT_MOST)
	// {
	// // Can't be bigger than...
	// height = Math.min(desiredHeight, heightSize);
	// }
	// else
	// {
	// // Be whatever you want
	//
	// height = desiredHeight;
	// }
	// setMeasuredDimension(width, height);
	// }
	// else
	// {
	// Log.w("ResourceHeader", "not set!");
	// super.setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
	// }
	//
	// // MUST CALL THIS
	//
	// }
}
